package com.wsq.demo01.controller;

import com.wsq.demo01.mapper.StudentMapper;
import com.wsq.demo01.pojo.StudentInfo;
import com.wsq.demo01.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class StudentController {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TestService testService;

    // 测试
    @RequestMapping("/test1")
    public String test1() {
        return "test1 is success";
    }

    // 添加信息
    @GetMapping("/add")
    public String addStudent() {
        if (studentMapper.testSelectByName("赵四") == null) {
            studentMapper.addStudentInfo(new StudentInfo(4, "赵四", "123456789", "软件工程系", "卓越2班", "女"));
            return "add is success";
        } else {
            return "student is Existence";
        }
    }

    // 查询信息
    @GetMapping("/list")
    public List<StudentInfo> queryList() {
        List<StudentInfo> studentInfos = studentMapper.queryStudentList();
        for (StudentInfo studentInfo : studentInfos) {
            System.out.println(studentInfo);
        }
        return studentInfos;
    }

    /* 测试从接口获取参数删除信息 */
    @GetMapping("/deleteByName/{name}")
    public String deleteStudent(@PathVariable("name") String name) {
        if (studentMapper.testSelectByName(name) != null) {
            studentMapper.deleteStudent(name);
            return "delete is success";
        } else {
            return "name is not exists,delete is fail";
        }
    }

    /* 测试从接口获取参数name查询 */
    @RequestMapping("/testByName/{name}")
    public StudentInfo selectByName(@PathVariable("name") String name) {
        StudentInfo studentInfo = new StudentInfo();
        if (studentMapper.testSelectByName(name) != null) {
            return studentMapper.testSelectByName(name);
        } else {
            studentInfo.setName("name is not exists");
            return studentInfo;
        }
    }

    /* 测试从接口获取参数新增 */
    @RequestMapping("/addTest/{id}/{name}/{Stu_id}/{Depart}/{grade}/{sex}")
    public String addTest(@PathVariable("id") int id, @PathVariable("name") String name,
                          @PathVariable("Stu_id") String Stu_id, @PathVariable("Depart") String Depart,
                          @PathVariable("grade") String grade, @PathVariable("sex") String sex) {
        if (studentMapper.testSelectByName(name) == null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("name", name);
            map.put("Stu_id", Stu_id);
            map.put("Depart", Depart);
            map.put("grade", grade);
            map.put("sex", sex);
            studentMapper.testAdd(map);
            return "success";
        } else {
            return "add is fail";
        }
    }

    /* 测试从接口获取参数修改 */
    @RequestMapping("/updateTest/{name}/{grade}")
    public String updateTest(@PathVariable("name") String name, @PathVariable("grade") String grade) {
        if (studentMapper.testSelectByName(name) != null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", name);
            map.put("grade", grade);
            studentMapper.testUpdate(map);
            return "update is success";
        } else {
            return "update is fail";
        }
    }

    // httpServletRequest获取前端数据
    @GetMapping("/tt")
    public StudentInfo list(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        return studentMapper.testSelectByName(httpServletRequest.getParameter("name"));
    }

    /* redis缓存：id查询 */
    @RequestMapping("/r1")
    public Map<String, Object> findUserById(@RequestParam int id) {
        StudentInfo studentInfo = testService.findUserById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("id", studentInfo.getId());
        result.put("name", studentInfo.getName());
        result.put("stuid", studentInfo.getStuId());
        result.put("depart", studentInfo.getDepart());
        result.put("grade", studentInfo.getGrade());
        result.put("sex", studentInfo.getSet());
        return result;
    }

    /* redis缓存：name查询 */
    @RequestMapping("/r2")
    public Map<String, Object> findUserByName(@RequestParam String name) {
        StudentInfo studentInfo = testService.findUserByName(name);
        Map<String, Object> result = new HashMap<>();
        result.put("id", studentInfo.getId());
        result.put("name", studentInfo.getName());
        result.put("stuid", studentInfo.getStuId());
        result.put("depart", studentInfo.getDepart());
        result.put("grade", studentInfo.getGrade());
        result.put("sex", studentInfo.getSet());
        return result;
    }

    /*redis缓存：删除*/
    @GetMapping("/d1/{name}")
    public String deleteStudent2(@PathVariable("name") String name) {
        if (studentMapper.testSelectByName(name) != null) {
            studentMapper.deleteStudent(name);
            String key = "user_" + name;
            redisTemplate.delete(key);
            return "delete is success";
        } else {
            return "name is not exists,delete is fail";
        }
    }

    /* redis缓存：修改 */
    @RequestMapping("/u1/{name}/{grade}")
    public String updateTest2(@PathVariable("name") String name, @PathVariable("grade") String grade) {
        String key = "user_" + name;
        ValueOperations<String, StudentInfo> operations = redisTemplate.opsForValue();
        if (studentMapper.testSelectByName(name) != null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("name", name);
            map.put("grade", grade);
            studentMapper.testUpdate(map);
            StudentInfo studentInfo = studentMapper.testSelectByName(name);
            System.out.println("---------------------更新写入缓存---------------------");
            operations.set(key, studentInfo, 5, TimeUnit.HOURS);
            return "update is success";
        } else {
            return "update is fail";
        }
    }

}
